﻿using LowCoding.Common;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace LowCoding.API
{
    /// <summary>
    /// 全局异常过滤
    /// </summary>
    public class GlobalExceptionFilterAttribute : Attribute, IExceptionFilter
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="actionExecutedContext"></param>
        public void OnException(ExceptionContext actionExecutedContext)
        {
            //记录异常日志
            if (actionExecutedContext.Exception != null)
            {
                LogHelper.WriteErrorLog(actionExecutedContext.Exception);
            }
            actionExecutedContext.Result = new JsonResult(ToJsonTran.Fail((int)ResponseCodeEnum.Error, actionExecutedContext.Exception?.Message ?? "服务器异常", null));
        }
    }
}